package com.ruoyi.project.config.qiniu.config;

import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.ruoyi.project.config.qiniu.domain.QiniuProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

@Configuration
public class QiniuConfig {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Bean
    public QiniuProperties qiniuProperties() {
        String query = "SELECT accessKey, secretKey, zone, bucket, domain " +
                "FROM resources_config " +
                "WHERE name = '七牛云' " +
                "AND status = '启用'" +
                "LIMIT 1";

        try {
            return jdbcTemplate.queryForObject(query, new RowMapper<QiniuProperties>() {
                @Override
                public QiniuProperties mapRow(ResultSet rs, int rowNum) throws SQLException {
                    QiniuProperties properties = new QiniuProperties();
                    properties.setAccessKey(rs.getString("accessKey"));
                    properties.setSecretKey(rs.getString("secretKey"));
                    properties.setZone(rs.getString("zone"));
                    properties.setBucket(rs.getString("bucket"));
                    properties.setDomain(rs.getString("domain"));
                    return properties;
                }
            });
        } catch (Exception e) {
            System.err.println("No configuration found for Qiniu Cloud");
            throw new RuntimeException("No configuration found for Qiniu Cloud", e);
        }
    }

    /**
     * 配置空间的存储区域
     */
    @Bean
    public com.qiniu.storage.Configuration qiNiuConfig() throws Exception {
        QiniuProperties qiniuProperties = qiniuProperties();

        switch (qiniuProperties.getZone()) {
            case "huadong":
                return new com.qiniu.storage.Configuration(Zone.huadong());
            case "huabei":
                return new com.qiniu.storage.Configuration(Zone.huabei());
            case "huanan":
                return new com.qiniu.storage.Configuration(Zone.huanan());
            case "beimei":
                return new com.qiniu.storage.Configuration(Zone.beimei());
            default:
                throw new Exception("存储区域配置错误");
        }
    }

    /**
     * 构建一个七牛上传工具实例
     */
    @Bean
    public UploadManager uploadManager() throws Exception {
        return new UploadManager(qiNiuConfig());
    }

    /**
     * 认证信息实例
     */
    @Bean
    public Auth auth() {
        QiniuProperties qiniuProperties = qiniuProperties();
        return Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
    }

    /**
     * 构建七牛空间管理实例
     */
    @Bean
    public BucketManager bucketManager() throws Exception {
        return new BucketManager(auth(), qiNiuConfig());
    }

    @Bean
    public Gson gson() {
        return new Gson();
    }

}
